package com.szf.account.web;

import java.util.Date;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.szf.account.entity.Account;
import com.szf.account.service.AccountService;
import com.szf.base.dao.Page;
import com.szf.base.exception.GenericException;
import com.szf.base.web.BaseActionSupport;
import com.szf.base.web.WebUtil;
import com.szf.util.MD5Util;
import com.szf.util.UuidGenerator;

@Namespace("/")
@Results({ @Result(name = "success", location = "/WEB-INF/jsp/account/account.jsp"), @Result(name = "input", location = "/WEB-INF/jsp/account/account-input.jsp") })
public class AccountAction extends BaseActionSupport {
	private Account account;

	@Autowired
	private AccountService accountService;

	@Override
	public String execute() {
		return super.execute();
	}

	public void list() {
		Page page = accountService.queryByIbatis(warpRequest(request), "sinlog.listAccount");
		WebUtil.renderJson(response, wrapResult(page));
	}

	public String add() {
		account = new Account();
		account.setCreateDate(new Date());
		account.setAccountId(UuidGenerator.getUuid());
		method = "add";
		return "input";
	}

	public String view() {
		account = accountService.findById(account.getAccountId());
		method = "view";
		return "input";
	}

	public String edit() {
		account = accountService.findById(account.getAccountId());
		method = "edit";
		return "input";
	}

	public void update() {
		try {
			if (account.getPassword().length() != 32) {
				account.setPassword(MD5Util.getStringMD5(account.getPassword()));
			}
			if ("add".equals(method)) {
				Account tmp = accountService.findByName(account.getUserName());
				if (tmp != null) {
					throw new GenericException("用户名重复！");
				}
			}
			accountService.save(account);
			ajaxDefaultResponse();
		} catch (Exception e) {
			ajaxErrorResponse(e.getMessage());
			logger.error(e.getMessage(), e);
		}
	}

	public void delete() {
		try {
			String ids = request.getParameter("ids");
			accountService.deleteAccount(ids);
			ajaxDefaultResponse();
		} catch (Exception e) {
			ajaxErrorResponse(e.getMessage());
			logger.error(e.getMessage(), e);
		}
	}

	public void use() {
		try {
			account = accountService.findById(account.getAccountId());
			account.setStatus("0");
			accountService.save(account);
			ajaxDefaultResponse();
		} catch (Exception e) {
			ajaxErrorResponse(e.getMessage());
			logger.error(e.getMessage(), e);
		}
	}

	public void unuse() {
		try {
			account = accountService.findById(account.getAccountId());
			account.setStatus("1");
			accountService.save(account);
			ajaxDefaultResponse();
		} catch (Exception e) {
			ajaxErrorResponse(e.getMessage());
			logger.error(e.getMessage(), e);
		}
	}

	public Account getAccount() {
		return account;
	}

	public void setAccount(Account account) {
		this.account = account;
	}

}
